<?php
namespace api\modules\v1\controllers;
use api\modules\v1\controllers\CoreController;
use common\models\Balance;
use common\models\BalanceLog;
use common\models\GroupWinning;
use common\models\Member;
use common\models\WithdrawLog;


/*
	账本(积分) 相关控制器
*/

class BalanceController extends CoreController
{

	/*
		获取用户余额 收益等信息

	*/
	public function actionGetBalance()
	{
		//账本类型
		$btype = $this->request('btype',1);
		$data = Balance::wallet($this->_memberId,$btype);
		$this->out('我的钱包',$data);
	}



	/*
		积分明细
		page 当前页
		page_size 每页数量
	*/
	public function actionBalanceLog()
	{
		//账本类型
		$btype = $this->request('btype');

		$whereArr = array();
		$where = ['member_id'=>$this->_memberId];
		if($btype && $btype<>6) $where['balance_type'] = $btype;
		$whereArr['where'] = $where;
        $select = $this->request('select');
		$params = array(
			'page'  => $this->request('page',1),
			'limit' => $this->request('page_size',10),
			'order' => 'id desc',
            'type'  => $btype,
            'select'=> $select,
		);
		$data = BalanceLog::dataList($whereArr, $params);
		$this->out('财务日志', $data);
	}

	/*
		用户账本列表
	*/
	public function actionBalanceList()
	{
		$data = Balance::getAllBalance($this->_memberId); 
		$this->out('账本列表', $data);
	}


    /*
    * 通过验证码修改密码
    * account   ## 账号 邮箱或是手机号
    * password  密码
    * code  验证码
    * type 1 登录密码  2交易密码
*/
    public function actionEditPass()
    {
//		## 验证码验证
        $account = $this->request('account');
//		$code   = $this->request('code');
//		if(!SmsModel::validataVcode($account,$code,1)) $this->error('验证码错误或是已经过期');
        $type = $this->request('type',1);
        $password = $this->request('password');
        $mibao1 = $this->request('mibao1');
        $mibao2 = $this->request('mibao2');

        if (\Yii::$app->cache->get('encrypted1'.$this->_memberId)!=$mibao1){
            $this->error('密保问题1错误');
        }

        if (\Yii::$app->cache->get('encrypted2'.$this->_memberId)!=$mibao2) {
            $this->error('密保问题2错误');
        }
        $member = Member::find()->where(['member_id'=>$this->_memberId])->one();
        if($type == 1){
//            if ($member->loginpwd!=md5($password)) $this->error('请输入正确的登录密码');
            $member->loginpwd = md5($this->request('new_password'));
        }else{
//            if ($member->paypwd!=md5($password)) $this->error('请输入正确的支付密码');
            $member->paypwd = md5($this->request('new_password'));
        }
        $member->update_time = time();
        $flag = $member->save(false);

        if(!$flag) $this->error('修改失败');
        $this->out('修改成功');

    }
    public function actionEncrypted(){
        $data=[
            1=>'你的生日?',
            2=>'你的性别?'
        ];
        return $this->out('密保问题',$data);
    }
    public function actionFee(){
        $count_fee      = WithdrawLog::find()->sum('fee');
        $this->out('基金',$count_fee);
    }

}